package dgut.party.business.mockdata;

import dgut.party.business.common.enumeration.Gender;
import dgut.party.business.dao.rbac.AuthorityRepository;
import dgut.party.business.dao.rbac.RoleRepository;
import dgut.party.business.dao.user.AccountRepository;
import dgut.party.business.dao.user.UserTitleRepository;
import dgut.party.business.entity.rbac.Role;
import dgut.party.business.entity.user.AdminUser;
import dgut.party.business.entity.user.NormalUser;
import dgut.party.business.entity.user.UserTitle;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class MockUsersBean {
  public static void initAccounts(
    AccountRepository accountRepo,
    UserTitleRepository titleRepo,
    RoleRepository roleRepo,
    AuthorityRepository authRepo,
    BCryptPasswordEncoder encoder) {
    Set<UserTitle> titles = userTitles();
    titleRepo.saveAll(titles);

    Set<Role> adminRoles = new HashSet<Role>();
    Set<Role> normalUserRoles = new HashSet<Role>();
    adminRoles.add(roleRepo.findByName("ADMIN"));
    normalUserRoles.add(roleRepo.findByName("NORMAL_USER"));

    NormalUser normalUser = NormalUser.builder().userTitle(titleRepo.findByName("党支部负责人").get(0)).build();
    AdminUser adminUser = AdminUser.builder().userTitle(titleRepo.findByName("管理员").get(0)).build();

    normalUser.setUsername("user");
    normalUser.setPassword(encoder.encode("user"));
    normalUser.setRealName("钟文泽");
    normalUser.setGender(Gender.MALE);
    normalUser.setQq("a404921113");
    normalUser.setWeixin("weixin1");
    normalUser.setAvatar("../../../assets/img/league-badge.jpg");
    normalUser.setUserTitle(titleRepo.findByName("党支部负责人").get(0));
    normalUser.setRoles(normalUserRoles);

    adminUser.setUsername("admin");
    adminUser.setPassword(encoder.encode("admin"));
    adminUser.setRealName("乐珈彤");
    adminUser.setGender(Gender.FEMALE);
    adminUser.setQq("b404921113");
    adminUser.setWeixin("weixin2");
    adminUser.setRoles(adminRoles);

    accountRepo.saveAll(Arrays.asList(adminUser, normalUser));
  }

  public static Set<UserTitle> userTitles() {
    Set<UserTitle> set = new HashSet<UserTitle>();
    set.add(UserTitle.builder().name("管理员").build());
    set.add(UserTitle.builder().name("党支部负责人").build());
    set.add(UserTitle.builder().name("党支部书记").build());
    return set;
  }
}
